package com.example.emailcraft.controller;

import com.example.emailcraft.entity.common.Result;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/api/v1")
public class UtilController {
    @GetMapping("/getJs")
    public ResponseEntity<Result<Object>> getJs() {
        return ResponseEntity.ok(
                new Result<>().setCode(HttpStatus.OK.value()).setMessage(
                        "<script>\n" +
                                "  // 初始化倒计时\n" +
                                "  document.querySelectorAll('.element[data-type=\"countdown\"]').forEach(element => {\n" +
                                "    const uuid = element.dataset.uuid;\n" +
                                "    if (!uuid) return;\n" +
                                "\n" +
                                "    // 确保 fetch 的 URL 正确\n" +
                                "    fetch(`http://localhost:9990/api/v1/getCountdown?uuid=${uuid}`)\n" +
                                "      .then(response => {\n" +
                                "        // 检查响应是否成功\n" +
                                "        if (!response.ok) {\n" +
                                "          throw new Error(`HTTP error! status: ${response.status}`);\n" +
                                "        }\n" +
                                "        return response.json();\n" +
                                "      })\n" +
                                "      .then(data => {\n" +
                                "        const endTime = data.endTime;\n" +
                                "        if (!endTime) return;\n" +
                                "\n" +
                                "        const countdownContainer = element.querySelector('.countdown-container');\n" +
                                "        const daysElement = countdownContainer.querySelector('#days');\n" +
                                "        const hoursElement = countdownContainer.querySelector('#hours');\n" +
                                "        const minutesElement = countdownContainer.querySelector('#minutes');\n" +
                                "        const secondsElement = countdownContainer.querySelector('#seconds');\n" +
                                "\n" +
                                "        function updateCountdown() {\n" +
                                "          const remainingTime = endTime - Date.now();\n" +
                                "\n" +
                                "          if (remainingTime <= 0) {\n" +
                                "            clearInterval(element.countdownInterval);\n" +
                                "            daysElement.textContent = '00';\n" +
                                "            hoursElement.textContent = '00';\n" +
                                "            minutesElement.textContent = '00';\n" +
                                "            secondsElement.textContent = '00';\n" +
                                "            return;\n" +
                                "          }\n" +
                                "\n" +
                                "          const newDays = Math.floor(remainingTime / 86400000);\n" +
                                "          const newHours = Math.floor((remainingTime % 86400000) / 3600000);\n" +
                                "          const newMinutes = Math.floor((remainingTime % 3600000) / 60000);\n" +
                                "          const newSeconds = Math.floor((remainingTime % 60000) / 1000);\n" +
                                "\n" +
                                "          daysElement.textContent = newDays.toString().padStart(2, '0');\n" +
                                "          hoursElement.textContent = newHours.toString().padStart(2, '0');\n" +
                                "          minutesElement.textContent = newMinutes.toString().padStart(2, '0');\n" +
                                "          secondsElement.textContent = newSeconds.toString().padStart(2, '0');\n" +
                                "        }\n" +
                                "\n" +
                                "        updateCountdown();\n" +
                                "        element.countdownInterval = setInterval(updateCountdown, 1000);\n" +
                                "      })\n" +
                                "      .catch(error => {\n" +
                                "        console.error('获取倒计时失败:', error);\n" +
                                "      });\n" +
                                "  });\n" +
                                "</script>"
                )
        );
    }
}